﻿using YiShop.System.Services;

namespace YiShop.System.Controllers
{
    [Route("system/role")]
    public class SysRoleController : ControllerBase
    {
        private readonly SysRoleService _sysRoleService;
        public SysRoleController(SysRoleService sysRoleService)
        {
            _sysRoleService = sysRoleService;
        }
        /// <summary>
        /// 查询角色信息表列表
        /// </summary>
        [HttpGet("list")]
        public async Task<SqlSugarPagedList<SysRoleDto>> GetSysRoleList([FromQuery] SysRoleDto dto)
        {
            return await _sysRoleService.GetDtoPagedListAsync(dto);
        }

        /// <summary>
        /// 获取 角色信息表 详细信息
        /// </summary>
        [HttpGet("{id}")]
        public async Task<AjaxResult> Get(long id)
        {
            //await _sysRoleService.CheckRoleDataScopeAsync(id);
            var data = await _sysRoleService.GetDtoAsync(id);
            return AjaxResult.Success(data);
        }


        /// <summary>
        /// 新增 角色信息表
        /// </summary>
        [HttpPost("")]
        public async Task<AjaxResult> Add([FromBody] SysRoleDto role)
        {
            if (!await _sysRoleService.CheckRoleNameUniqueAsync(role))
            {
                return AjaxResult.Error($"新增角色'{role.RoleName}失败，角色名称已存在");
            }
            else if (!await _sysRoleService.CheckRoleKeyUniqueAsync(role))
            {
                return AjaxResult.Error($"新增角色'{role.RoleName}失败，角色权限已存在");
            }

            var data = await _sysRoleService.InsertRoleAsync(role);
            return AjaxResult.Success(data);
        }

        [HttpPut("")]
        public async Task<AjaxResult> Edit([FromBody] SysRoleDto role)
        {
            if (!await _sysRoleService.CheckRoleNameUniqueAsync(role))
            {
                return AjaxResult.Error($"新增角色'{role.RoleName}失败，角色名称已存在");
            }
            else if (!await _sysRoleService.CheckRoleKeyUniqueAsync(role))
            {
                return AjaxResult.Error($"新增角色'{role.RoleName}失败，角色权限已存在");
            }

            var data = await _sysRoleService.InsertRoleAsync(role);
            return AjaxResult.Success(data);
        }

        [HttpPut("changeStatus")]
        public async Task<AjaxResult> ChangeStatus([FromBody] SysRoleDto role)
        {
            var data = await _sysRoleService.UpdateRoleStatusAsync(role);
            return AjaxResult.Success(data);
        }

    }
}